<?php
namespace App\Providers\VersionTraits;

use App\Models\System\RolePermission;
use App\Models\WxUniversity;
use App\Wen\Utils\Settings;
use Dcat\Admin\Models\AdminSubMenu;
use Dcat\Admin\Models\Menu;
use Dcat\Admin\Models\Permission;
use Dcat\Admin\Models\Role;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

trait _1_2_10_1_trait{

    function _v1_2_0_1(){
        if(!Schema::hasTable('wx_shop_coupons')) {
            Schema::create('wx_shop_coupons', function (Blueprint $table) {
                $table->id();
                // 1: 固定金额 2：比例
                $table->unsignedTinyInteger('type')->default(1)->comment('优惠类型');
                $table->float('num')->default(0)->comment('优惠数额');
                $table->float('min_purchase')->comment('最低购买金额');
                $table->string('goods')->nullable()->comment('适用特定商品');
                $table->string('classifies')->nullable()->comment('适用商品分类');
                $table->string('shops')->nullable()->comment('适用店铺');
                $table->string('users')->nullable()->comment('适用用户');
                $table->dateTime('start_time')->comment('使用开始时间');
                $table->dateTime('end_time')->comment('使用结束时间');
                $table->unsignedTinyInteger('status')->comment('是否激活');
                $table->timestamps();
            });
        }
        if(!Schema::hasColumn('wx_posts', 'has_fields')){
            Schema::table('wx_posts', function (Blueprint $table) {
                $table->unsignedTinyInteger('has_fields')->after('video_id')->nullable()->comment('是否有fields');
            });
        }

    }

    function _v1_2_0_2(){
//        if(!Menu::where('uri', '/shop/coupon')->exists()){
//            $admin_menu = new Menu();
//            $admin_menu->parent_id = 36;
//            $admin_menu->order =  12;
//            $admin_menu->title = '优惠劵';
//            $admin_menu->uri = '/shop/coupon';
//            $admin_menu->extension = '';
//            $admin_menu->show = 1;
//            $admin_menu->save();
//        }
    }

    function _v1_2_0_3(){

        if(!AdminSubMenu::where('uri', '/student_certify')->exists()){
            $admin_menu = new AdminSubMenu();
            $admin_menu->parent_id =0;
            $admin_menu->order = 1012;
            $admin_menu->icon = 'fa-fw fa-soccer-ball-o';
            $admin_menu->title = '学生认证';
            $admin_menu->uri = '/student_certify';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }

        AdminSubMenu::where('uri', '/settings/safe')->where('order', '<>', 9999999997)->update(['order'=>9999999997]);
        AdminSubMenu::where('title', '库')->where('parent_id', 0)->orWhere('id', Settings::get('sub_menu_ku_parent_id', 0, false, false))->where('order', '<>', 9999999998)->update(['order'=>9999999998]);
        AdminSubMenu::where('uri', '/page/settings')->where('order', '<>', 9999999999)->update(['order'=>9999999999]);
    }

    function _v1_2_0_4(){
        global $__MINI_GLOBAL_PAYCODE_25__;

        if($__MINI_GLOBAL_PAYCODE_25__) {
            if (!Schema::hasColumn('wx_pets_adoption', 'video_url')) {
                Schema::table('wx_pets_adoption', function (Blueprint $table) {
                    $table->string('video_cover')->after('pictures')->nullable()->comment('视频封面');
                    $table->string('video_url')->after('pictures')->nullable()->comment('视频链接');
                });
            }
        }

        if(!Schema::hasColumn('wx_used_good', 'video_url')){
            Schema::table('wx_used_good', function (Blueprint $table) {
                $table->string('video_cover')->after('image_urls')->nullable()->comment('视频封面');
                $table->string('video_url')->after('image_urls')->nullable()->comment('视频链接');
            });
        }

    }

    function _v1_2_0_6(){
        if(!Schema::hasColumn('wx_voter', 'is_sticky')){
            Schema::table('wx_voter', function (Blueprint $table) {
                $table->unsignedTinyInteger('is_sticky')->after('tenant_id')->default(0)->comment('是否置顶');
            });
        }
    }

    function _v1_2_0_8(){
        $table_info_columns = DB::select( DB::raw('SHOW COLUMNS FROM wx_voter'));
        foreach($table_info_columns as $column){
            if($column->Field == 'content'){
                $col_type = $column->Type;
                if($col_type == 'varchar(255)' || $col_type == 'text'){
                    Schema::table('wx_voter', function (Blueprint $table) {
                        $table->longText('content')->change();
                    });
                }
                break;
            }
        }

        if(Permission::where('http_path', 'files/uploads*')->exists()){
            if(Permission::where('http_path', 'files/uploads*,files/ueditor/uploads*')->exists()){
                Permission::where('http_path', 'files/uploads*')->delete();
            }else{
                Permission::where('http_path', 'files/uploads*')->update([
                    'http_path' => 'files/uploads*,files/ueditor/uploads*',
                    'slug' => 'files_uploads,files_ueditor_uploads'
                ]);
            }
        }

    }

    function _v1_2_0_9(){
        if(!Schema::hasColumn('wx_shop_order_goods', 'buyer_user_id')){
            Schema::table('wx_shop_order_goods', function (Blueprint $table) {
                $table->unsignedBigInteger('buyer_user_id')->nullable()->after('seller_user_id')->comment('买家用户');
            });
        }
    }


    function _v1_2_0_10(){
        if(!Menu::where('uri', '/mp_free_publish')->exists()){
            $admin_menu = new Menu();
            $admin_menu->parent_id = 18;
            $admin_menu->order =  43;
            $admin_menu->title = '公众号推送';
            $admin_menu->uri = '/mp_free_publish';
            $admin_menu->extension = '';
            $admin_menu->show = 1;
            $admin_menu->save();
        }

    }


    function _v1_2_0_11(){

        if(!Schema::hasColumn('wx_shop_order_coupon', 'scanner')){
            Schema::table('wx_shop_order_coupon', function (Blueprint $table) {
                $table->dateTime('scan_at')->nullable()->after('barcode')->comment('核销时间');
                $table->unsignedBigInteger('scanner')->nullable()->after('barcode')->comment('核销用户');
            });
        }

        if(!Schema::hasTable('wx_mp_custom_menu')) {
            Schema::create('wx_mp_custom_menu', function (Blueprint $table) {
                $table->id();

                $table->integer('parent_id')->default(0)->comment('父级id');
                $table->integer('order')->default(0)->comment('排序');

                $table->string('type', 36)->comment('响应动作类型');
                $table->string('name', 50)->comment('菜单标题');
                $table->string('key')->nullable()->comment('事件key');
                $table->string('url')->nullable()->comment('网页链接');

                $table->string('media_id', 128)->nullable()->comment('素材media_id');
                $table->string('appid', 30)->nullable()->comment('小程序的appid');
                $table->string('pagepath')->nullable()->comment('小程序的页面路径');

                $table->string('article_id', 128)->nullable()->comment('发布后article_id');

                $table->unsignedTinyInteger('depth')->default(1)->comment('层级');
                $table->timestamps();
            });
        }

        if(!Schema::hasTable('wx_mp_message')) {
            Schema::create('wx_mp_message', function (Blueprint $table) {
                $table->id();
                $table->string('msgtype', 20)->comment('消息类型');
                $table->string('content', 5000)->nullable()->comment('消息内容');

                $table->string('img_url')->nullable()->comment('图片url');
                $table->string('video_url')->nullable()->comment('视频url');
                $table->string('voice_url')->nullable()->comment('语音url');
                $table->string('thumb_url')->nullable()->comment('封面url');
                $table->string('musicurl')->nullable()->comment('音乐地址');

                $table->string('media_id', 128)->nullable()->comment('素材id');
                // 视频封面，音乐封面
                $table->string('thumb_media_id', 128)->nullable()->comment('封面素材id');
                // 视频标题，音乐标题，图文标题
                $table->string('title')->nullable()->comment('标题');
                // 视频标题，音乐描述，图文描述
                $table->string('description')->nullable()->comment('描述');
                $table->string('hqmusicurl')->nullable()->comment('高质音乐地址');
                // 图文外链
                $table->string('url')->nullable()->comment('图文外链');
                // 图文封面， 共用 thumb_url
//                $table->string('picurl')->nullable()->comment('图文封面');
                // 内部图文消息id
                $table->string('article_id', 128)->nullable()->comment('图文article_id');
                // 卡卷消息
                $table->string('card_id', 50)->nullable()->comment('卡卷id');
                $table->timestamps();
            });
        }

        if(!Schema::hasTable('wx_mp_message_reply')) {
            Schema::create('wx_mp_message_reply', function (Blueprint $table) {
                $table->id();
                // 1: 全匹配 2：包含匹配（用户的关键词被囊括）
                $table->string('word')->index('index_1')->comment('触发词');
                $table->unsignedTinyInteger('type')->default(1)->comment('匹配关系');
                $table->unsignedBigInteger('mp_message_id')->comment('回复消息id');
                $table->unsignedInteger('order')->default(0)->comment('权重（越大越靠前）');
                $table->unsignedTinyInteger('status')->default(1)->comment('是否启用');
            });
        }

    }


    function _v1_2_0_12(){
        $flag = false;
        if(!Schema::hasTable('wx_mp_free_publish')) {
            $flag = true;
        }else if(Schema::hasTable('wx_mp_free_publish') && !Schema::hasColumn('wx_mp_free_publish', 'thumb_url')){
            Schema::dropIfExists('wx_mp_free_publish');
            $flag = true;
        }
        if($flag){
            Schema::create('wx_mp_free_publish', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('parent_id')->default(0)->comment('父级id');
                $table->bigInteger('order')->default(0)->comment('排序');
                $table->string('media_id', 128)->unique('index_0')->nullable()->comment('素材id');
                $table->string('title')->comment('标题');
                $table->string('author')->nullable()->comment('作者');
                $table->string('digest')->nullable()->comment('摘要');
                $table->longText('content')->comment('内容');
                $table->string('content_source_url')->nullable()->comment('原文链接');
                $table->string('thumb_url')->comment('封面图片');
                $table->string('thumb_media_id', 128)->comment('图文消息的封面图片素材id');
                $table->unsignedTinyInteger('need_open_comment')->default(1)->comment('开启评论');
                $table->unsignedTinyInteger('only_fans_can_comment')->default(0)->comment('是否粉丝才可评论');
                $table->string('pic_crop_235_1', 30)->nullable()->comment('封面裁剪为2.35:1规格的坐标字段');
                $table->string('pic_crop_1_1', 30)->nullable()->comment('封面裁剪为1:1规格的坐标字段');
                $table->dateTime('add_draft_time')->nullable()->comment('同步到草稿');
                $table->dateTime('hope_publish_time')->nullable()->comment('定时发布');
                $table->string('publish_id', 20)->nullable()->comment('发布任务id');
                // 0:成功, 1:发布中，2:原创失败, 3: 常规失败, 4:平台审核不通过, 5:成功后用户删除所有文章, 6: 成功后系统封禁所有文章
                $table->unsignedTinyInteger('publish_status')->nullable()->comment('发布状态');
                $table->string('article_id', 128)->nullable()->comment('文章id');
                $table->string('article_url')->nullable()->comment('文章链接');
                $table->timestamps();
            });
        }
    }

    function _v1_2_0_13(){
        $table_info_columns = DB::select( DB::raw('SHOW COLUMNS FROM wx_voter_player'));
        foreach($table_info_columns as $column){
            if($column->Field == 'img_urls'){
                $col_type = $column->Type;
                if($col_type == 'varchar(500)'){
                    Schema::table('wx_voter_player', function (Blueprint $table) {
                        $table->string('img_urls', 10000)->change();
                    });
                }
                break;
            }
        }

    }


    function _v1_2_0_14(){
        global $__MINI_GLOBAL_TENANT_ID__;
        if($__MINI_GLOBAL_TENANT_ID__ === 0){
            if(!(Role::where('slug', 'enterprise_admin')->exists())){
                $role = new Role();
                $role->slug = 'enterprise_admin';
                $role->name = '商城管理员';
                $r = $role->save();
                if($r){
                    foreach (['shop_add_goods', 'shop_save_goods', 'shop_goods', 'files_uploads,files_ueditor_uploads'] as $p_slug){
                        $per_id = Permission::where('slug', $p_slug)->value('id');
                        if($per_id && $per_id > 0){
                            $role_permiss = new RolePermission();
                            $role_permiss->role_id = $role->id;
                            $role_permiss->permission_id = $per_id;
                            $role_permiss->save();
                        }
                    }
                }
            }
        }
    }

    function _v1_2_0_15(){
        if(!Schema::hasColumn('wx_posts', 'is_only_text')){
            Schema::table('wx_posts', function (Blueprint $table) {
                $table->unsignedTinyInteger('is_only_text')->after('is_wechat_sph')->nullable()->comment('简单文本');
            });
        }
        if(!Schema::hasColumn('wx_comment', 'is_only_text')){
            Schema::table('wx_comment', function (Blueprint $table) {
                $table->unsignedTinyInteger('is_only_text')->after('is_sticky')->nullable()->comment('简单文本');
            });
        }
    }


    function _v1_2_0_18(){

        if (!Menu::where('uri', 'menu_note_parent')->exists()) {
            $admin_menu = new Menu();
            $admin_menu->icon = ' fa-paper-plane-o';
            $admin_menu->parent_id = 0;
            $admin_menu->order = 55;
            $admin_menu->title = '恋爱纸条';
            $admin_menu->extension = '';
            $admin_menu->uri = 'menu_note_parent';
            $admin_menu->show = 1;
            $admin_menu->save();
            $parent_id = $admin_menu->id;
            Settings::set('menu_note_parent_id', $parent_id);

            if (!Menu::where('uri', '/note/config')->exists()) {
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order = 0;
                $admin_menu->title = '配置';
                $admin_menu->uri = '/note/config';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

        }

        if(!Schema::hasTable('wx_note')) {
            Schema::create('wx_note', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('user_id')->index('index_0')->comment('用户id');
                $table->string('anonymous_name')->nullable()->comment('匿名名字');
                $table->string('anonymous_avatar')->nullable()->comment('匿名头像');

                $table->text('content')->nullable()->comment('内容');
                $table->json('image_urls')->nullable()->comment('图片数组');
                $table->string('video_url')->nullable()->comment('视频地址');
                $table->string('video_cover')->nullable()->comment('视频封面');
                $table->string('tags')->nullable()->comment('标签');

                $table->double('longitude', 10, 6)->nullable()->comment('经度');
                $table->double('latitude', 10, 6)->nullable()->comment('纬度');
                $table->string('address_name')->nullable()->comment('地址名');
                $table->string('address_detailed')->nullable()->comment('地址名');

                $table->string('ip')->nullable()->comment('ip地址');
                $table->string('country')->nullable()->comment('国家');
                $table->string('province')->index('index_1')->nullable()->comment('省份');
                $table->string('city')->nullable()->index('index_2')->comment('城市');
                $table->string('district')->nullable()->index('index_3')->comment('县城');

                $table->unsignedTinyInteger('is_anonymous')->default(0)->comment('是否匿名');
                $table->unsignedTinyInteger('status')->default(0)->comment('状态');
                $table->timestamps();
            });
        }

    }

    function _v1_2_0_19(){

        if(!Schema::hasColumn('wx_user', 'club_id')){
            Schema::table('wx_user', function (Blueprint $table) {
                $table->unsignedBigInteger('club_id')->nullable()->after('user_state')->unique()->comment('俱乐部id');
            });
        }

        if(!Schema::hasColumn('wx_shop_contact', 'club_id')){
            Schema::table('wx_shop_contact', function (Blueprint $table) {
                $table->unsignedBigInteger('club_id')->nullable()->after('shop_id')->index('index_2')->comment('俱乐部id');
            });
        }

        if (!Menu::where('uri', 'menu_bureau_parent')->exists()) {
            $admin_menu = new Menu();
            $admin_menu->icon = ' fa-star-half-empty';
            $admin_menu->parent_id = 0;
            $admin_menu->order = 56;
            $admin_menu->title = '组局活动';
            $admin_menu->extension = '';
            $admin_menu->uri = 'menu_bureau_parent';
            $admin_menu->show = 1;
            $admin_menu->save();
            $parent_id = $admin_menu->id;
            Settings::set('menu_bureau_parent', $parent_id);

            if (!Menu::where('uri', '/bureau/config')->exists()) {
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order = 0;
                $admin_menu->title = '配置';
                $admin_menu->uri = '/bureau/config';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

            if (!Menu::where('uri', '/clubs')->exists()) {
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order = 0;
                $admin_menu->title = '俱乐部';
                $admin_menu->uri = '/clubs';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

            if (!Menu::where('uri', '/bureau/list')->exists()) {
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order = 1;
                $admin_menu->title = '活动';
                $admin_menu->uri = '/bureau/list';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

            if (!Menu::where('uri', '/bureau/comment')->exists()) {
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order = 2;
                $admin_menu->title = '留言';
                $admin_menu->uri = '/bureau/comment';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

            if (!Menu::where('uri', '/bureau/orderGoods')->exists()) {
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order = 3;
                $admin_menu->title = '订单';
                $admin_menu->uri = '/bureau/orderGoods';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

            if (!Menu::where('uri', '/bureau/refund')->exists()) {
                $admin_menu = new Menu();
                $admin_menu->parent_id = $parent_id;
                $admin_menu->order = 4;
                $admin_menu->title = '退款';
                $admin_menu->uri = '/bureau/refund';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }

        }

        if(!Schema::hasTable('wx_club')) {
            Schema::create('wx_club', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('user_id')->index('index_0')->comment('用户id');

                $table->string('avatar')->comment('头像');
                $table->string('avatar_tmp')->nullable()->comment('审核中头像');

                $table->string('name')->comment('名称');
                $table->string('name_tmp')->nullable()->comment('审核中名称');

                $table->string('intro')->nullable()->comment('简介');
                $table->string('intro_tmp')->nullable()->comment('审核中简介');

                $table->string('club_background_maps')->nullable()->comment('背景图');
                $table->string('club_background_maps_tmp')->nullable()->comment('审核中背景图');


                $table->unsignedTinyInteger('is_authentication')->default(0)->comment('认证');
                $table->unsignedTinyInteger('is_official')->default(0)->comment('官方运营');


                $table->double('longitude', 10, 6)->nullable()->comment('经度');
                $table->double('latitude', 10, 6)->nullable()->comment('纬度');
                $table->string('address_name')->nullable()->comment('地址名');
                $table->string('address_detailed')->nullable()->comment('地址名');

                $table->string('ip')->nullable()->comment('ip地址');
                $table->string('country')->nullable()->comment('国家');
                $table->string('province')->index('index_1')->nullable()->comment('省份');
                $table->string('city')->nullable()->index('index_2')->comment('城市');
                $table->string('district')->nullable()->index('index_3')->comment('县城');

                // 0: 审核中 1: 正常
                $table->unsignedTinyInteger('status')->default(0)->comment('状态');
                $table->string('black_reason')->nullable()->comment('拉黑原因');
                $table->timestamps();
            });
        }

        if(!Schema::hasTable('wx_club_team')) {
            Schema::create('wx_club_team', function (Blueprint $table) {
                $table->id();
                // 1：自主创建 2：系统临时创建
                $table->unsignedTinyInteger('type')->default(1)->comment('类型');
                // 队长
                $table->unsignedBigInteger('user_id')->nullable()->index('index_0')->comment('用户id');
                $table->unsignedBigInteger('club_id')->index('index_1')->comment('俱乐部id');
                $table->unsignedBigInteger('bureau_id')->nullable()->index('index_4')->comment('活动id');
                $table->unsignedBigInteger('group_id')->nullable()->index('index_2')->comment('组id');
                $table->string('avatar')->nullable()->comment('头像');
                $table->string('name')->nullable()->comment('名称');
                $table->string('intro')->nullable()->nullable()->comment('简介');
                // ['id','id','id']
                $table->string('members')->index('index_3')->nullable()->comment('队员');
                $table->unsignedInteger('members_num')->default(0)->comment('成员数量');
                // 0: 无限制
                $table->unsignedInteger('members_limit')->default(0)->comment('成员数量限制');
                $table->unsignedTinyInteger('is_lock')->default(0)->comment('是否锁住');
                // 1：有效 0: 失效
                $table->unsignedTinyInteger('status')->default(1)->comment('状态');
                $table->timestamps();
            });
        }

        if(!Schema::hasTable('wx_club_follow')) {
            Schema::create('wx_club_follow', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('user_id')->index('index_0')->comment('用户id');
                $table->unsignedBigInteger('club_id')->index('index_1')->comment('俱乐部id');
                $table->unsignedTinyInteger('status')->default(1)->comment('状态( 1: 关注中， 0 已取消 )');
                $table->timestamps();
            });
        }

        if(!Schema::hasTable('wx_bureau')) {
            Schema::create('wx_bureau', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('user_id')->index('index_0')->comment('用户id');
                $table->unsignedBigInteger('club_id')->index('index_1')->comment('俱乐部id');
                // 1: 活动 2：个人比赛 3：战队比赛
                $table->unsignedTinyInteger('type')->default(1)->comment('类型');
                $table->string('title')->comment('标题');
                $table->string('intro', 1000)->comment('活动说明');
                $table->longText('content')->comment('活动详细');

                $table->json('image_urls')->comment('图片数组');
                $table->string('video_url')->nullable()->comment('视频地址');
                $table->string('video_cover')->nullable()->comment('视频封面');
                $table->string('tags')->nullable()->comment('标签');
                $table->unsignedDecimal('least_price', 10)->default(0)->comment('最低费用');

                // 活动时间，比赛时间
                $table->string('service_date_start', 10)->nullable()->comment('服务开始日期');
                $table->string('service_date_end', 10)->nullable()->comment('服务结束日期');
                $table->string('service_time_start', 8)->nullable()->comment('服务开始时间');
                $table->string('service_time_end', 8)->nullable()->comment('服务结束时间');

                $table->double('longitude', 10, 6)->nullable()->comment('经度');
                $table->double('latitude', 10, 6)->nullable()->comment('纬度');
                $table->string('address_name')->nullable()->comment('地址名');
                $table->string('address_detailed')->nullable()->comment('地址名');

                $table->string('ip')->nullable()->comment('ip地址');
                $table->string('country')->nullable()->comment('国家');
                $table->string('province')->index('index_2')->nullable()->comment('省份');
                $table->string('city')->nullable()->index('index_3')->comment('城市');
                $table->string('district')->nullable()->index('index_4')->comment('县城');

                $table->unsignedTinyInteger('refund_policy')->default(255)->comment('退款政策');
                // 1: 公开（所有地方可见） 2:私密（不出现在首页） 3: 私密（仅分享链接可见）
                $table->unsignedTinyInteger('exposure_type')->default(1)->comment('曝光类型');
                $table->json('user_info_fill')->nullable()->comment('用户信息收集');

                $table->string('pre_sale_reply')->nullable()->comment('售前自动回复');
                $table->string('pre_sale_reply_img')->nullable()->comment('售前自动回复图片');
                $table->string('after_sale_reply')->nullable()->comment('售后自动回复');
                $table->string('after_sale_reply_img')->nullable()->comment('售后自动回复图片');

                $table->unsignedTinyInteger('is_notice_before')->default(0)->comment('活动开始通知');
                $table->unsignedTinyInteger('is_notice_after')->default(0)->comment('活动结束通知');
                $table->unsignedTinyInteger('is_auto_ship')->default(0)->comment('未核销处理');
                $table->string('md5', 32)->index('index_5')->comment('md5');

                // 0: 审核中 1：正常 2：下架 3：管理员下架 4举报下架
                $table->unsignedTinyInteger('status')->default(0)->comment('状态');

                $table->timestamps();
            });
        }


        // 分组 -> 分队
        if(!Schema::hasTable('wx_bureau_group')) {
            Schema::create('wx_bureau_group', function (Blueprint $table) {
                $table->id();
                $table->string('name')->nullable()->comment('分组名称');
                $table->unsignedBigInteger('user_id')->index('index_0')->comment('用户id');
                $table->unsignedBigInteger('bureau_id')->index('index_1')->comment('活动id');
                $table->unsignedBigInteger('club_id')->index('index_2')->comment('俱乐部id');
                // 1: 活动 2：个人比赛 3：战队比赛 （不能二次更改）
                $table->unsignedTinyInteger('type')->default(1)->comment('类型');
                $table->unsignedInteger('total_people')->default(0)->comment('活动总人数');
                $table->unsignedDecimal('cost_per_person', 10)->default(0)->comment('每人费用');
                $table->unsignedInteger('num_teams')->default(0)->comment('战队数量');
                $table->unsignedInteger('people_per_team')->default(0)->comment('每队人数');
                $table->unsignedDecimal('cost_per_team', 10)->default(0)->comment('每队费用');
                $table->unsignedTinyInteger('is_waiting')->default(0)->comment('是否满座后可候补等位');
                $table->unsignedTinyInteger('is_limit_one')->default(0)->comment('每人限购一次');
                $table->unsignedTinyInteger('is_real_name')->default(0)->comment('强制实名认证');

                $table->dateTime('signup_start_time')->comment('报名开始时间');
                $table->dateTime('signup_end_time')->comment('报名结束时间');
                // 0: 不限制 1：男 2：女 3: 男女1:1
                $table->unsignedTinyInteger('signup_gender')->default(0)->comment('报名性别');
                // 1: 所有人 2: 仅俱乐部成员
                $table->unsignedTinyInteger('signup_scope')->default(1)->comment('报名范围');

            });
        }


        if(!Schema::hasTable('wx_bureau_collect')){
            Schema::create('wx_bureau_collect', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('user_id')->comment('用户id');
                $table->unsignedBigInteger('bureau_id')->comment('活动id');
                $table->unsignedBigInteger('club_id')->index('index_1')->comment('俱乐部id');
                // ( 1: 收藏， 0 取消收藏 )
                $table->unsignedTinyInteger('status')->default(1)->comment('状态');
                $table->index(['user_id', 'bureau_id'], 'index_0');
                $table->timestamps();
            });
        }

        if(!Schema::hasTable('wx_bureau_comment')){
            Schema::create('wx_bureau_comment', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('user_id')->index('index_0')->comment('用户id');
                $table->unsignedBigInteger('bureau_id')->index('index_1')->comment('活动id');
                $table->unsignedBigInteger('comment_id')->default(0)->index('index_2')->comment('父级id');
                $table->unsignedBigInteger('comment_agent_id')->default(0)->nullable()->comment('回复的用户id');
                $table->string('comment_agent_name')->nullable()->comment('回复的用户名');
                $table->text('comment_content')->comment('评论内容');
                $table->string('comment_img_url')->nullable()->comment('评论图片');

                $table->double('longitude', 10, 6)->nullable()->comment('经度');
                $table->double('latitude', 10, 6)->nullable()->comment('纬度');

                $table->string('ip')->nullable()->comment('ip地址');
                $table->string('country')->nullable()->comment('国家');
                $table->string('province')->nullable()->comment('省份');
                $table->string('city')->nullable()->comment('城市');
                $table->string('district')->nullable()->comment('县城');
                $table->unsignedTinyInteger('is_sticky')->default(0)->comment('是否置顶');
                $table->unsignedTinyInteger('comment_state')->default(0)->comment('状态：0:审核中，1:审核通过，2:驳回');
                $table->timestamps();
            });
        }


        if(!Schema::hasColumn('wx_wallpapers', 'bureau_id')){
            Schema::table('wx_wallpapers', function (Blueprint $table) {
                $table->unsignedBigInteger('bureau_id')->nullable()->after('subject_id')->comment('活动id');
                $table->string('district')->nullable()->index('index_3')->after('user_id')->comment('县城');
                $table->string('city')->nullable()->index('index_2')->after('user_id')->comment('城市');
                $table->string('province')->nullable()->index('index_1')->after('user_id')->comment('省份');
                $table->string('country')->nullable()->after('user_id')->comment('国家');
                $table->string('ip')->nullable()->after('user_id')->comment('ip地址');
                $table->string('address_detailed')->nullable()->after('user_id')->comment('地址名');
                $table->string('address_name')->nullable()->after('user_id')->comment('地址名');
                $table->double('latitude', 10, 6)->nullable()->after('user_id')->comment('纬度');
                $table->double('longitude', 10, 6)->nullable()->after('user_id')->comment('经度');
            });
        }

        $table_order_info_columns = DB::select( DB::raw('SHOW COLUMNS FROM wx_shop_order'));
        foreach($table_order_info_columns as $column){
            if($column->Field == 'user_remark'){
                $col_type = $column->Type;
                if($col_type == 'varchar(255)'){
                    Schema::table('wx_shop_order', function (Blueprint $table) {
                        $table->string('user_remark', 5000)->change();
                    });
                }
                break;
            }
        }

        if(!Schema::hasColumn('wx_posts', 'bureaus')){
            Schema::table('wx_posts', function (Blueprint $table) {
                $table->string('bureaus', 100)->after('tags_id')->nullable()->comment('组局');
            });
        }

        if(!Schema::hasColumn('wx_shop_order_goods', 'refund_no')){
            Schema::table('wx_shop_order_goods', function (Blueprint $table) {
                $table->string('refund_account', 64)->nullable()->after('refund_deadline')->comment('退款入账账户');
                $table->string('refund_time', 20)->nullable()->after('refund_deadline')->comment('退款时间');
                $table->float('refund_amount')->nullable()->after('refund_deadline')->comment('退款金额');
                $table->unsignedTinyInteger('refund_from')->nullable()->after('refund_deadline')->comment('退款平台（0：微信 1：支付宝）');
                $table->string('refund_no', 64)->nullable()->index('index_3')->after('refund_deadline')->comment('退款单号');
            });
        }

        if(!Schema::hasColumn('wx_shop_order', 'serial_platform_type')){
            Schema::table('wx_shop_order', function (Blueprint $table) {
                $table->string('serial_platform_type', 10)->nullable()->after('serial_platform')->comment('支付产品类型');
            });
        }

        if(!Schema::hasColumn('wx_shop_order', 'order_goods_id')){
            Schema::table('wx_shop_order', function (Blueprint $table) {
                $table->string('order_goods_id')->index('index_2')->nullable()->after('order_from')->comment('包含的商品（仅作模糊查询）');
            });
        }


        if(!Schema::hasColumn('wx_order', 'order_serial_platform_type')){
            Schema::table('wx_order', function (Blueprint $table) {
                $table->string('order_serial_platform_type', 10)->after('order_serial_platform')->nullable()->comment('支付产品类型');
            });
        }

    }


    function _v1_2_1_1(){
        if(!Schema::hasColumn('wx_bureau', 'order')){
            Schema::table('wx_bureau', function (Blueprint $table) {
                $table->unsignedInteger('order')->after('md5')->default(0)->comment('排序');
            });
        }
    }

    function _v1_2_1_2(){
        // todo:
        if(!AdminSubMenu::where('uri', '/settings/pagemine')->exists()){
            $page_settings_id = AdminSubMenu::where('uri', '/page/settings')->value('id');
            if($page_settings_id > 0){
                $admin_menu = new AdminSubMenu();
                $admin_menu->parent_id = $page_settings_id;
                $admin_menu->order =  1020;
                $admin_menu->icon = 'fa-forumbee';
                $admin_menu->title = '用户与登录';
                $admin_menu->uri = '/settings/pagemine';
                $admin_menu->extension = '';
                $admin_menu->show = 1;
                $admin_menu->save();
            }
        }
    }

    function _v1_2_1_3(){
        $table_info_columns = DB::select( DB::raw('SHOW COLUMNS FROM wx_mp_message'));
        foreach($table_info_columns as $column){
            if($column->Field == 'content'){
                $col_type = $column->Type;
                if($col_type == 'varchar(255)'){
                    Schema::table('wx_mp_message', function (Blueprint $table) {
                        $table->string('content', 5000)->change();
                    });
                }
                break;
            }
        }
    }

    function _v1_2_1_4(){
        if(!Schema::hasTable('wx_posts_weight_refresh')){
            Schema::create('wx_posts_weight_refresh', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('post_id')->comment('笔记id');
                $table->dateTime('expect_time')->comment('期待刷新时间');
                $table->dateTime('refresh_time')->nullable()->comment('实际刷新时间');
                $table->unsignedTinyInteger('status')->default(0);
                $table->index(['expect_time', 'status'], 'index_0');
            });
        }
    }

    function _v1_2_1_7(){
        if(!Schema::hasColumn('wx_user_withdrawal', 'id_card')){
            Schema::table('wx_user_withdrawal', function (Blueprint $table) {
                $table->string('id_card', 18)->after('real_name')->nullable()->comment('身份证号');
                $table->string('ali_batch_id', 50)->after('account_at')->nullable()->index('index_1')->comment('支付宝批次号');
                $table->string('wx_batch_id', 50)->after('account_at')->nullable()->index('index_2')->comment('微信批次号');
                $table->string('account_way', 10)->after('account_at')->nullable()->comment('到账方式');
                $table->string('withdrawal_id', 20)->after('id')->nullable()->unique('index_3')->comment('提现编号');
                $table->unsignedTinyInteger('is_lock')->default(0)->after('updated_at')->comment('是否锁住');
            });
        }

        if(!Schema::hasColumn('wx_user_financial', 'real_name')){
            Schema::table('wx_user_financial', function (Blueprint $table) {
                $table->string('alipay_phone')->nullable()->after('bank_card')->comment('支付宝手机号');
                $table->string('wechat_phone')->nullable()->after('bank_card')->comment('微信手机号');
                $table->string('id_card', 18)->nullable()->after('bank_card')->comment('身份证号');
                $table->string('real_name')->nullable()->after('bank_card')->comment('真实名字');
            });
        }
    }


    function _v1_2_1_9(){
        if(!Schema::hasColumn('wx_shop_classify', 'platform_division')){
            Schema::table('wx_shop_classify', function (Blueprint $table) {
                $table->float('platform_division')->default(0)->after('guarantee')->comment('平台分成%');
            });
        }
    }

    function _v1_2_1_10(){
        if(!Schema::hasColumn('wx_posts', 'tenant_obj')){
            Schema::table('wx_posts', function (Blueprint $table) {
                $table->string('tenant_obj', 600)->nullable()->after('tenant_id')->comment('分站对象');
            });
        }
    }


    function _v1_2_1_11(){
        if(!Schema::hasColumn('wx_circle', 'tenant_show')){
            Schema::table('wx_circle', function (Blueprint $table) {
                $table->tinyInteger('tenant_show')->default(-1)->after('list_style')->comment('展示分站');
            });
        }

        if(!(WxUniversity::where('sid', -1)->exists())){
            $university = new WxUniversity();
            $university->sid = -1;
            $university->name = '全域';
            $university->order = 9999999;
            $university->private = 0;
            $university->is_certify = 0;
            $university->save();
        }

        if(!Schema::hasColumn('wx_attachment', 'cos_review')){
            Schema::table('wx_attachment', function (Blueprint $table) {
                // 0：正常。1：确认为当前场景的违规内容。2：疑似为当前场景的违规内容
                $table->unsignedTinyInteger('cos_review')->default(3)->after('store_type')->comment('cos审核结果 0:正常 1:违规 2:疑似 3:未送审 4:送审中 5:待查询结果');
            });
        }

        if(!Schema::hasTable('wx_tmp_meta')){
            Schema::create('wx_tmp_meta', function (Blueprint $table) {
                $table->id();
                $table->string('type_obj_id')->comment('类型_对象id');
                $table->string('meta_key')->comment('键');
                $table->string('meta_value')->comment('值');
                $table->unique(['type_obj_id', 'meta_key'], 'index_0');
                $table->timestamps();
            });
        }


    }

}
